import SyntaxHighlighter from '@/components/SyntaxHighlighter'
import { useEffect } from 'react'

const arr = [1, 2, 3, 4, 5, 6]
const k = 3

const code1 = `
  // 传入 [1, 2, 3, 4, 5, 6] 数组和一个旋转的次数 k，返回旋转后的数据
  // 例如：[1, 2, 3, 4, 5, 6]，k = 3；=> [4, 5, 6, 1, 2, 3]
`

const code2 = `
  const setFn = () => {
    const result = arr.slice(k)
    for (let i = 0; i < k; i++ ) {
      result.push(arr[i])
    }

    console.log('result', result)
    return result;
  }
`

function ReversalArray() {
  const setFn = () => {
    const result = arr.slice(k)
    for (let i = 0; i < k; i++ ) {
      result.push(arr[i])
    }

    console.log('result', result)
    return result;
  }


  useEffect(() => {
    setFn(arr, k)
  }, [])

  return (
    <div>
      <p>在指定反转的次数内反转数组</p>
      <SyntaxHighlighter children={code1} />
      <p>代码实现逻辑：</p>
      <SyntaxHighlighter children={code2} />
    </div>
  )
}

export default ReversalArray